/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package todomanager.data;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.TreeMap;

/**
 * 
 * Representation of different Todo tasks represented in an ArrayList.
 * 
 * @author jonasbirgersson
 * 
 */
public class TodoData {

	private int keyID = 0;
	private ResourceBundle language;
	
	private Map<Integer, ITask> tasks = new TreeMap<Integer, ITask>();

	public void addTask(ITask newTask) {
		++keyID;
		newTask.setKey(keyID);
		tasks.put(keyID, newTask);
		
		// tasks.put(newTask.hashCode(), newTask);
	}
	/**
	 * @author gihoon kim
	 * 
	 */
	
	public void editTask(ITask newTask, ArrayList help){
		String helpstr = help.get(0).toString();
		int help1 = Integer.parseInt(helpstr);
		tasks.remove(help1+1);
		tasks.put(help1+1, newTask);
	}
	/**
	 * 
	 * @param index
	 *            of the task to be deleted.
	 */
	public void deleteTask(int id) {
		tasks.remove((Integer) id);
		
	}
	/**
	 * Dirty fix to delete tasks from the table since it doesn't sort data that isn't shown in the table
	 * @param name
	 */
	public void deleteTaskByName(String name){
		Set<Integer> allTasks = tasks.keySet();
		Iterator<Integer> it = allTasks.iterator();
		while (it.hasNext()) {
			ITask temp = tasks.get(it.next());
			if(temp.getName().equals(name)) {
				deleteTask(temp.getKey());
				return;
			}
			
		}
		
	}

	/**
	 * 
	 * 
	 * @param id
	 *            for the task to be fetched.
	 * @return the corresponding task
	 */
	public ITask getTask(int id) {
		return tasks.get((Integer) id);
	}

	@Override
	public String toString() {
		String toReturn = "";
		Set<Integer> allTasks = tasks.keySet();
		Iterator<Integer> it = allTasks.iterator();
		while (it.hasNext()) {
			toReturn += tasks.get(it.next()).toString();
			if (it.hasNext()) {
				toReturn += "\n";
			}
		}
		return toReturn;
	}

	/**
	 * Returns the database as an object for the main Table.
	 * 
	 * @return the database
	 */
	public Object[][] getTasksAsObject(ResourceBundle lang) {
		this.language = lang;
		Object[][] tempDataObject = new Object[tasks.size()][8];
		Set<Integer> allTasks = tasks.keySet();
		Iterator<Integer> it = allTasks.iterator();
		int i = 0;
		while (it.hasNext()) {
			ITask temp = tasks.get(it.next());
			tempDataObject[i][0] = temp.isDone();
			tempDataObject[i][1] = temp.getName();
			tempDataObject[i][2] = temp.getDescription();
			tempDataObject[i][3] = temp.getDueDate();
			tempDataObject[i][4] = temp.getCategory();
			tempDataObject[i][5] = new Integer(temp.getPriority());
			tempDataObject[i][6] = new Integer(temp.getProgress());
			tempDataObject[i][7] = temp.getKey();
			// tempDataObject[i][7] = temp.hashCode();
			i++;
		}
		i--;
		while(i>=0){
			if(this.language.getString("language").equals("Swedish")){
				String temp = (String) tempDataObject[i][4];
				if(temp.equals("Meeting"))
					tempDataObject[i][4] = "Möte";
				else if(temp.equals("Assignment"))
					tempDataObject[i][4] = "Uppdrag";
				else if(temp.equals("Class"))
					tempDataObject[i][4] = "Klass";
			}
			else{
				String temp = (String) tempDataObject[i][4];
				if(temp.equals("Möte"))
					tempDataObject[i][4] = "Meeting";
				else if(temp.equals("Uppdrag"))
					tempDataObject[i][4] = "Assignment";
				else if(temp.equals("Klass"))
					tempDataObject[i][4] = "Class";
			}
			i--;
		}

		return tempDataObject;
	}

	/**
	 * 
	 * @return the database
	 */
	public ArrayList<ITask> getDataAsArrayList() {
		ArrayList<ITask> tmpList = new ArrayList<ITask>();
		Set<Integer> allTasks = tasks.keySet();
		Iterator<Integer> it = allTasks.iterator();
		int i = 0;
		while (it.hasNext()) {
			ITask temp = tasks.get(it.next());
			tmpList.add(temp);
		}
		return tmpList;
	}

	/**
	 * 
	 * @param days
	 *            number of days left before due.
	 * @return the names of the tasks that corresponds to the input
	 */
	public ArrayList getPendingTasks(int days) {
		ArrayList pendingTasks = new ArrayList();
		Set<Integer> allTasks = tasks.keySet();
		Iterator<Integer> it = allTasks.iterator();

		while (it.hasNext()) {
			ITask tempTask = tasks.get(it.next());
			String tmpDate;
			tmpDate = tempTask.getDueDate();
			int daysLeft = getDaysLeft(tmpDate);
			if (daysLeft >= 0 && daysLeft <= days) {
				pendingTasks.add(tempTask.getName());
			}

		}
		return pendingTasks;
	}

	/**
	 * 
	 * @param date
	 *            takes the date. Needs to be in the format (yyyy-MM-dd).
	 * @return number of days between the input date and the current date
	 */
	public int getDaysLeft(String date) {
		String tmpString[] = date.split("-");
		int compareYear = new Integer(tmpString[0]);
		int compareMonth = new Integer(tmpString[1]);
		int compareDay = new Integer(tmpString[2]);

		int currentYear = Calendar.getInstance().get(Calendar.YEAR);
		// Calendar.MONTH has the format 0 Jan, 1 Feb, 2 Mar... Hence the + 1.
		int currentMonth = Calendar.getInstance().get(Calendar.MONTH) + 1;
		int currentDay = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);

		Calendar currentDate = Calendar.getInstance();
		Calendar compareDate = Calendar.getInstance();
		currentDate.set(currentYear, currentMonth, currentDay);
		compareDate.set(compareYear, compareMonth, compareDay);

		long diff = compareDate.getTimeInMillis()
				- currentDate.getTimeInMillis();
		return (int) (diff / (24 * 60 * 60 * 1000));

	}

}
